#!/usr/bin/env python
#coding=utf-8
#dujie

from __future__ import division
import time
import sys
from define import BASE_DIR,BLACK_LIST,route_key as route_list
sys.path.append(BASE_DIR)

from lib.abstract_cron import abstract_cron
from lib.func_ext import dateTypeInterval,str2sec,sec2str,ksort

#需要修改
class api_ftss_day(abstract_cron) :

    #需要修改
    noLock = True   #是否生成脚本锁文件
    insertTable = 'sdb_statistics_api_ftss_day'

    def __init__(self) :
        super(api_ftss_day,self).__init__(__file__)
        self.mongo_stat = self.objCore.mongo('mongodb_stat')
        dbConf_stat = self.setting.get('mongodb_stat',{})
        self.mongo_spider = self.objCore.mongo('mongodb_spider')
        dbConf_spider = self.setting.get('mongodb_spider',{})
        mongoDB_life = dbConf_stat.get('mongo_db_life')
        mongoDB_spider = dbConf_spider.get('mongo_db_spider')
        mongoBizColl_retry = dbConf_spider.get('mongo_retry')

        #取昨天日期
        mongoBizColl_life = sec2str('%Y%m%d',time.time()-1*24*3600)

        self.bizColl_life = self.mongo_stat[mongoDB_life][mongoBizColl_life]
        self.bizColl_retry = self.mongo_spider[mongoDB_spider][mongoBizColl_retry]
        self.api_succ = {}
        self.apiAllCount= {}
        self.apiCount= {}
        self.retryCount ={}

    def boot(self,timestamp=[]) :
#        self.preBoot()


        year,month,day = (int(sec2str('%Y',time.time())),int(sec2str('%m',time.time())),int(sec2str('%d',time.time())) )
        time_stare = time.mktime((year,month,day-1,0,0,0,0,0,0))
        time_end = time.mktime((year,month,day-1,23,59,59,0,0,0))


        #time_stare = str2sec('%s-%s-%s %s:%s:%s' %(year,month,day-1,0,0,0) ,'%Y-%m-%d %H:%M:%S')
        #time_end =   str2sec('%s-%s-%s %s:%s:%s' %(year,month,day-1,23,59,59) ,'%Y-%m-%d %H:%M:%S')

        #mongo_msgs = self.bizColl_retry.find()
        mongo_msgs = self.bizColl_retry.find({'time':{'$gt':time_stare,'$lt':time_end}})
        retry_list = self.getRetryMsgIdList(mongo_msgs)


        for routeKey,api in route_list.items():
            #life_line当天的表里取出所有api数目
            all_api_liste = self.bizColl_life.find({'route_key':routeKey})
            all_api_count = all_api_liste.count()
            self.apiAllCount[api] = self.apiAllCount.get(api,0) + all_api_count     #一个api的总个数
            #life_line当天的表里取出所有成功api数目
            route_key_lists = self.bizColl_life.find({'route_key':routeKey,'shopex_finish': 1})
            route_key_count = route_key_lists.count()
            #从retry表里取出所有重试的次数
            retry_lists = self.bizColl_life.find({'route_key':routeKey,'shopex_finish': 1,'_id':{'$in':retry_list}})
            retry_count = retry_lists.count()
            self.retryCount[api] = self.retryCount.get(api,0) + (retry_count)
            #一个api成功的总个数
            self.apiCount[api] = self.apiCount.get(api,0) + (route_key_count-retry_count)

        #获取一次成功率
        self.apiftss()

        #将数据插入数据库
        for api in self.apiCount:
            tmpData = {}
            tmpData['api'] = api
            tmpData['retry_count'] = self.retryCount[api]
            tmpData['api_succ_once'] = self.apiCount[api]
            tmpData['api_sum'] = self.apiAllCount[api]
            tmpData['api_ftss'] = self.api_succ[api]
            tmpData['date'] = str(sec2str('%Y%m%d',time.time()-1*24*3600))
            tmpData['calltime'] = str2sec(time.strftime('%Y-%m-%d'),'%Y-%m-%d')-1*24*3600
            self.filterItems(tmpData)

        self.doBatchExec()
        self.sufBoot()
        print 'ok'


    def apiftss (self):
        for api,v in self.apiCount.items():
            if self.apiAllCount[api] > 0:
                self.api_succ[api] =round( self.apiCount[api]/self.apiAllCount[api],5)
            else:
                self.api_succ[api] = 1.00000

        return self.api_succ


    def getRetryMsgIdList (self,mongo_msgs = {}):
        msg_id_list = []
        for msgs in mongo_msgs:
            msg_id_list.append(msgs['_id'])
        return msg_id_list


    def getTimeFilter(self,timestamp=[]) :
        keyFilter = {}
        keyFilter['date'] = timestamp and timestamp or (int(sec2str('%Y%m%d',time.time()-1*24*3600)))
        return keyFilter

    def insertKeys(self) :
        return ['api','retry_count','api_succ_once','api_sum','api_ftss','date','calltime']


#需要修改
if __name__ == '__main__' :
    obj = api_ftss_day()
    obj.boot()




